package com.weather;

import android.app.*;
import android.content.Intent;
import android.widget.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.os.Bundle;

/**
 * Activitate in care se afiseaza lista de orase
 *
 */
public class Weather extends ListActivity {
	
	CityAdapter adapter;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new CityAdapter(this);
        setListAdapter(adapter);
        
        //lista orase
    	getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    		
			public boolean onItemLongClick(AdapterView<?> listAdapter, View view, int position, long id) {
				adapter.remove(position);
				return true;
			}
		});
        
    	//buton adaugare oras
    	Button button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
		    public void onClick(View v) {
		    	Intent intent = new Intent ();
		    	intent.setClass(Weather.this, AddCity.class);
		    	startActivityForResult(intent, 1);
		    }
		});
    }
    
    @Override
    public void onActivityResult (int requestCode, int responseCode, Intent data) {
    	if(requestCode == 1 && responseCode == 1) {
    		adapter.addCity(data.getStringExtra("city"));
    	}
    }
    
    public void onPause() {
    	super.onPause();
    	adapter.saveList();
    }
    
    public void onResume() {
    	super.onResume();
    	adapter.loadList();
    }
    
    @Override
	public void onListItemClick(ListView list, View v, int position, long id) {
    	
    	//porneste o noua activitate pentru a afisa vremea
		Intent intent = new Intent ();
		intent.setClass(Weather.this, WeatherForecast.class);
		intent.putExtra("city", (String)(adapter.getItem(position)));
		startActivity(intent);
	}
}